package com.wm.iterator;

/**
 * @ClassName: ConcreteIterator
 * @Description: 迭代器接口具体实现
 * @Author: WM
 * @Date: 2023/5/8 9:22
 */
public class ConcreteIterator implements Iterator {
    private Collection collection;
    // 当前途代器遍历到的元素位置
    private int pos = -1;

    public ConcreteIterator(Collection collection) {
        this.collection = collection;
    }

    @Override
    public Object previous() {
        if (pos > 0) {
            pos--;
        }
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if (pos < collection.size() - 1) {
            pos++;
        }
        return collection.get(pos);
    }

    @Override
    public boolean hashNext() {
        if (pos < collection.size() - 1) {
            return true;
        }
        return false;
    }
}
